package com.ochnas.volunteersignup.domain;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;

@Entity
public class FilledVolunteerRequisition implements Serializable {
	@Id @GeneratedValue
	private Long id;
	
	@OneToOne
	@JoinColumn(name = "USER_ID", nullable = false) 
	private User user;
	@OneToOne
	@JoinColumn(name = "EVENT_ID", nullable = false) 
	private Event event;
	@OneToOne
	@JoinColumn(name = "POSITION_ID", nullable = false) 
	private Position position;
	@OneToOne
	@JoinColumn(name = "TIME_SLOT_ID", nullable = true) 
	private TimeSlot timeSlot;
	
	public FilledVolunteerRequisition() {
		super();
	}
	
	public FilledVolunteerRequisition(User user, Event event,
			Position position, TimeSlot timeSlot) {
		super();
		this.user = user;
		this.event = event;
		this.position = position;
		this.timeSlot = timeSlot;
	}


	public FilledVolunteerRequisition(User user, Event event, Position position) {
		super();
		this.user = user;
		this.event = event;
		this.position = position;
	}


	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Event getEvent() {
		return event;
	}
	public void setEvent(Event event) {
		this.event = event;
	}
	public Position getPosition() {
		return position;
	}
	public void setPosition(Position position) {
		this.position = position;
	}
	public TimeSlot getTimeSlot() {
		return timeSlot;
	}
	public void setTimeSlot(TimeSlot timeSlot) {
		this.timeSlot = timeSlot;
	}
	
	@Override
	public String toString() {
		StringBuffer strBuff = new StringBuffer("FilledVolunteerRequisition[id=");
		strBuff.append(id);
		strBuff.append("]{userId=");
		strBuff.append((user==null?"null":user.getId()));
		strBuff.append(",event=");
		strBuff.append((event==null?"null":event.getId()));
		strBuff.append(",position=");
		strBuff.append((position==null?"null":position.getId()));
		strBuff.append(",timeSlot=");
		strBuff.append((timeSlot==null?"null":timeSlot.getId()));
		return strBuff.toString();
	}	

    public String toXml() {  	
        Document response = DocumentFactory.getInstance().createDocument();
        Element root = response.addElement("filledVolunteerRequisition");
        root.addAttribute("id", String.valueOf(getId()));
        Element userElement = root.addElement("user")
        	.addAttribute("id", String.valueOf(getUser().getId()));
        Element eventElement = root.addElement("event")
        	.addAttribute("id", String.valueOf(getEvent().getId()));
        Element positionElement = root.addElement("position")
        	.addAttribute("id", String.valueOf(getPosition().getId()));
        if (getTimeSlot() != null) {
        	Element timeSlotElement = root.addElement("timeslot")
        	.addAttribute("id", String.valueOf(getTimeSlot().getId()));
        }
        return response.asXML();
    }
    
    public static FilledVolunteerRequisition fromXml(String xml) {
		try {
			Document doc = DocumentHelper.parseText(xml);
			
			long fId = Long.parseLong(doc.selectSingleNode("/filledVolunteerRequisition/@id").getStringValue());
			FilledVolunteerRequisition fvr = new FilledVolunteerRequisition();
			fvr.setId(fId);
			
			Node eNode = doc.selectSingleNode("/filledVolunteerRequisition/event");
			long eId = Long.parseLong(eNode.valueOf("@id"));
			Event e = new Event();
			e.setId(eId);
			fvr.setEvent(e);
			
			Node uNode = doc.selectSingleNode("/filledVolunteerRequisition/user");
			long uId = Long.parseLong(uNode.valueOf("@id"));
			User u = new User();
			u.setId(uId);
			fvr.setUser(u);
			
			Node pNode = doc.selectSingleNode("/filledVolunteerRequisition/position");
			long pId = Long.parseLong(pNode.valueOf("@id"));
			Position p = new Position();
			p.setId(pId);
			fvr.setPosition(p);
	            
			Node tNode = doc.selectSingleNode("/filledVolunteerRequisition/timeslot");
			if (tNode != null) {
				long tId = Long.parseLong(tNode.valueOf("@id"));
				TimeSlot t = new TimeSlot();
				t.setId(tId);
				fvr.setTimeSlot(t);
			}

			return fvr;
		
		} catch (DocumentException e) {
			throw new RuntimeException("Invalid event xml: " + xml, e);
		} catch (NumberFormatException e) {
			throw new RuntimeException("Invalid event xml: " + xml, e);
		} catch (Exception e) {
			throw new RuntimeException("Invalid event xml: " + xml, e);
		}
    }
    
}
